package com.eight.travel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.travel.dao.PlaceMapper;
import com.eight.travel.dao.PlaceOrderMapper;
import com.eight.travel.dao.SignMapper;
import com.eight.travel.exception.AddFailException;
import com.eight.travel.pojo.PlaceOrder;
import com.eight.travel.pojo.Placecom;
import com.eight.travel.pojo.Sign;
import com.eight.travel.utils.BaseResp;
import com.eight.travel.pojo.Place;
import com.eight.travel.service.PlaceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class PlaceServiceImpl implements PlaceService {

    @Resource
    private PlaceMapper placeMapper;
    @Resource
    private SignMapper signMapper;
    @Resource
    private PlaceOrderMapper placeOrderMapper;

    //分页查询所有景点信息
    public BaseResp findAll(int page, int size){
//        Page<Place> of = Page.of(page, size);
//        Page<Place> placePage = placeMapper.selectPage(of, null);
        List<Place> allByPage = placeMapper.findAllByPage((page-1)*size,size);
        System.out.println("allByPage = " + allByPage);
        List<Place> all = placeMapper.findAll();
        return new BaseResp().SUCCESS("景区信息",allByPage,all.size());
    }

    //传入景点id和点击率,修改点击率数据
    @Override
    public BaseResp updateClick(int id, int click) {
        int i = placeMapper.updateClick(id, click);
        if (i>-1){
            return new BaseResp().SUCCESS("点击率修改成功",null,null);
        }
        return new BaseResp().FAIL("点击率修改失败");
    }

    //用id查询景点的全部信息
    @Override
    public BaseResp findOne(int id) {
        Place one = placeMapper.findOne(id);
        System.out.println("one = " + one);
        return new BaseResp().SUCCESS("根据pid查询的用户信息",one,1);
    }

    //查看已开放的景点
    @Override
    public BaseResp findAllByChoiceOpen(int page, int size) {
        List<Place> allByChoiceOpen = placeMapper.findAllByChoiceOpen((page-1)*size, size);
        List<Place> all = placeMapper.findAllByOpen();
        return new BaseResp().SUCCESS("景区信息",allByChoiceOpen,all.size());
    }
    //模糊查询景点的名字
    @Override
    public BaseResp findByName(int page, int size,String placename) {
        List<Place> allByChoiceOpen = placeMapper.findByNamePage((page-1)*size, size,placename);
        List<Place> all = placeMapper.findByName(placename);
        return new BaseResp().SUCCESS("景区信息",allByChoiceOpen,all.size());
    }

    //模糊查询 根据景点名分页查询已开放的景区信息
    @Override
    public BaseResp findByNameAndOpenByPage(int page, int size, String placename) {
        List<Place> byNameAndOpenByPage = placeMapper.findByNameAndOpenByPage((page - 1) * size, size, placename);
        List<Place> byNameAndOpen = placeMapper.findByNameAndOpen(placename);
        System.out.println("byNameAndOpen.size() = " + byNameAndOpen.size());
        return new BaseResp().SUCCESS("景区信息",byNameAndOpenByPage,byNameAndOpen.size());
    }

    //根据pid查看所有评论
    @Override
    public BaseResp findAllBycomByPage(int pid,int page,int size) {
        //分页查询
        List<Placecom> allBycom = placeMapper.findAllBycomByPage(pid,(page-1)*size,size);
        //全查 获取总数
        List<Placecom> allBycom1 = placeMapper.findAllBycom(pid);
        return new BaseResp().SUCCESS("景区信息",allBycom,allBycom1.size());
    }

    //根据uid和pid添加评论
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResp addBycom(Placecom placecom) {
        try {
            int i = placeMapper.addBycom(placecom);
            if (i==1){
                int i1 = placeMapper.addBycompic(placecom.getPlacecompic());
                if (i1>-1){
                    return new BaseResp().SUCCESS("添加成功",placecom,1);
                }else {
                    throw new AddFailException("添加图片失败");
                }
            }else {
                throw new AddFailException("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;
    }
    //点击率排行榜
    @Override
    public BaseResp clickfirst() {
        List<Place> place=placeMapper.clickfirst();
        return new BaseResp().SUCCESS("景区信息",place,place.size());
    }

    //使用七牛云上传图片存入景点图片
    @Override
    public BaseResp insertPlacePic(String upload, int pid) {
      int i=placeMapper.insertPlacePic(upload,pid);
      if (i>-1){
          return  new BaseResp().SUCCESS("添加成功",null,null);
      }
        return null;
    }

    //打卡功能 是否打卡
    @Override
    public Boolean issign(int uid, String location, LocalDate date) {
        QueryWrapper<Sign> signQueryWrapper = new QueryWrapper<>();
       signQueryWrapper.eq("uid", uid).eq("location", location).eq("time", date.toString());
        Sign sign = signMapper.selectOne(signQueryWrapper);
        if (sign==null){
            return false;
        }
        return true;
    }

    //添加打卡信息
    @Override
    public Boolean addSing(Integer uid, String location, LocalDate date) {
        int insert = signMapper.insert(new Sign(0, uid, location, date.toString(), null));
        if (insert>-1){
            return true;
        }
        return false;
    }

    //后台管理 对于景区信息的修改
    @Override
    public Boolean updatePlace(Place place) {
        QueryWrapper<Place> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("pid",place.getPid());
        int update = placeMapper.update(place, objectQueryWrapper);
        if(update>-1){
            return true;
        }
        return false;
    }

    //后台管理 对于景区信息的新增
    @Override
    public Boolean insertPlace(Place place) {
        int insert = placeMapper.insert(place);
        if (insert>-1){
            return true;
        }
        return false;
    }

    //后台管理 对于景区信息的删除
    @Override
    public Boolean deletePlace(int pid) {
        int delete = placeMapper.deleteStatus(pid);
        if (delete>-1){
            return true;
        }
        return false;
    }

    //添加景区门票的订单
    @Override
    public Boolean addOrdersPlace(int uid, int pid) {
        //订单编号
        Random random = new Random();
        String replace="";
        for (int i=0;i<10;i++){
            int i1 = random.nextInt(9);
            replace+=i1;
        }
        //查询是否符合优惠条件  该用户去过不止一次,门票打八折
        Boolean isok=false;
        List<PlaceOrder> places = placeOrderMapper.selectList(new QueryWrapper<PlaceOrder>().eq("pid", pid).eq("uid", uid));
        //根据pid查询景区的信息
        Place place = placeMapper.selectOne(new QueryWrapper<Place>().eq("pid", pid));
        if (places.size()>1){
             isok=true;
        }
        Double money = place.getMoney();
        //如果符合优惠条件
        if (isok){
             money = money * 0.8;
        }
        int insert = placeOrderMapper.insert(new PlaceOrder(null,replace, uid, pid, new Date(),money,0,null));
        if (insert>-1){
            return true;
        }
        return false;
    }

    //退订景区门票的订单
    @Override
    public Boolean upDateOrdersPlace(int uid, int pid) {
        int i = placeOrderMapper.updateStatus(2,uid,pid);
        if (i>-1){
            return true;
        }
        return false;
    }

    //支付宝回调 修改订单状态为1
    @Override
    public Boolean updateOrdersfortwo(int oid) {
        int i = placeOrderMapper.updateOrdersfortwo(oid);
        if (i>-1){
            return true;
        }
        return false;
    }

    //查看已支付全部订单
    @Override
    public BaseResp findAllOrdersok(int page,int size,int uid) {
        List<PlaceOrder>  places= placeOrderMapper.findAllOrdersok((page-1)*size,size,uid);
        //已支付全部订单数
        List<PlaceOrder> countok = placeOrderMapper.countok(uid);
        return new BaseResp().SUCCESS("已支付全部订单",places,countok.size());
    }

    //查看未支付全部订单
    @Override
    public BaseResp findAllOrdersno(int page,int size,int uid) {
        List<PlaceOrder>  places= placeOrderMapper.findAllOrdersno((page-1)*size,size,uid);
        //未支付全部订单数
        List<PlaceOrder> countno = placeOrderMapper.countno(uid);
        return new BaseResp().SUCCESS("未支付全部订单",places,countno.size());
    }
}
